package data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashSet;

public class ReadAndWriteCareCode {
	private static File file = new File("historydata"+File.separator+"careStockCode.txt");
	private static File fileExl = new File("historydata"+File.separator+"excelStockCode.txt");
	private static LinkedHashSet<String> codeSet = new LinkedHashSet<String>();
	private static LinkedHashSet<String> ExlCodeSet = new LinkedHashSet<String>();
	
	static {
		try {
			if(file.exists()){
				FileReader fileReader = new FileReader(file);
				BufferedReader br = new BufferedReader(fileReader);
				String code ;
				codeSet.add("sh000001");
				codeSet.add("sz399001");
				while((code = br.readLine())!=null){
					codeSet.add(code);
				}
				br.close();
			}
			if(fileExl.exists()){
				FileReader fileReader = new FileReader(fileExl);
				BufferedReader br = new BufferedReader(fileReader);
				String code ;
				ExlCodeSet.add("sh000001");
				ExlCodeSet.add("sz399001");
				while((code = br.readLine())!=null){
					ExlCodeSet.add(code);
				}
				br.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static LinkedHashSet<String> getExlCodeSet() {
		return ExlCodeSet;
	}
	
	private static void writeCode() {
		try {
			FileWriter fileWriter = new FileWriter(file);
			file.delete();
			BufferedWriter bw = new BufferedWriter(fileWriter);
			for (Iterator<String> iterator = codeSet.iterator(); iterator.hasNext();) {
				String aCode = iterator.next();
				bw.write(aCode);
				bw.newLine();
			}
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	private static void writeExlCode() {
		try {
			FileWriter fileWriter = new FileWriter(fileExl);
			fileExl.delete();
			BufferedWriter bw = new BufferedWriter(fileWriter);
			for (Iterator<String> iterator = ExlCodeSet.iterator(); iterator.hasNext();) {
				String aCode = iterator.next();
				bw.write(aCode);
				bw.newLine();
			}
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static void addCode(String code){
		if(!codeSet.contains(code)){
			codeSet.add(code);
			SaleAndBuyPool.addCode(code);
			writeCode();
		}
	}
	
	public static void removeCode(String code){
		codeSet.remove(code);
		SaleAndBuyPool.removeCode(code);
		writeCode();
	}
	
	public static void addExlCode(String code){
		if(!ExlCodeSet.contains(code)){
			ExlCodeSet.add(code);
			writeExlCode();
		}
	}
	
	public static void removeExlCode(String code){
		ExlCodeSet.remove(code);
		writeExlCode();
	}
	
	public static LinkedHashSet<String> getCodeSet(){
		return codeSet;
	}
}
